﻿using Script.UI;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine.UI;
using MTUnity.Utils;
using Script.UI.Component;
using UnityEngine;
using System;
using XCharts;

public class UI_Temperature : UIUpdateBase
{
    public HistogramGroup Panel_6H;
    public HistogramGroup Panel_12H;
    
    public LiquidChart Liquid_Temperature;
    public Text Txt_Temperature;
    public Text Txt_RefreshTime;

    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.Warehouse.Current_Temperature , UpdateCurrentTemperature },
            {JsonFildName.Warehouse.Record_Temperature , UpdateRecordTemperature },
            {JsonFildName.Warehouse.TemperatureRefreshTime , UpdateRefreshTime },
        };
    }
    #endregion

    #region UpdateInterface
    public void UpdateCurrentTemperature(JsonData jsonData)
    {
        if (jsonData.IsDouble)
        {
            UpdateCurrentTemperature(JsonUtils.ToDouble(jsonData));
        }
        
    }

    public void UpdateRecordTemperature(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            //List<HistogramDataBase> listData = Utils.SetData<HistogramData_Temperature>(jsonData, JsonFildName.Warehouse.Temperature);
            double dMax1 = 0;
            double dMin1 = 0;
            double dMax2 = 0;
            double dMin2 = 0;
            List<HistogramDataBase> listData = new List<HistogramDataBase>();
            for (int i = 0; i < jsonData.Count; i++)
            {
                HistogramDataBase data = new HistogramData_Temperature()
                {
                    
                    Current = JsonUtils.ToDouble(jsonData[i][JsonFildName.Warehouse.Temperature]),
                    Timestamp = JsonUtils.ToLong((long)jsonData[i][JsonFildName.Common.Timestamp]),
                };
                if (i < 6)
                {
                    dMax1 = Math.Max(dMax1, Math.Abs(JsonUtils.ToDouble(jsonData[i][JsonFildName.Warehouse.Temperature])));
                    dMin1 = Math.Min(dMin1, Math.Abs(JsonUtils.ToDouble(jsonData[i][JsonFildName.Warehouse.Temperature])));
                }
                dMax2 = Math.Max(dMax2, Math.Abs(JsonUtils.ToDouble(jsonData[i][JsonFildName.Warehouse.Temperature])));
                dMin2 = Math.Min(dMin2, Math.Abs(JsonUtils.ToDouble(jsonData[i][JsonFildName.Warehouse.Temperature])));
                listData.Add(data);
            }
            for (int i = 0; i < listData.Count; i++)
            {
                ((HistogramData_Temperature)listData[i]).Max = i < 6 ? dMax1 : dMax2;
                ((HistogramData_Temperature)listData[i]).Min = i < 6 ? dMin1 : dMin2;
            }
            UpdateRecordTemperature(listData);
        }
        
    }
    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }
    #endregion

    private void UpdateCurrentTemperature(double dCurrentTemperature)
    {
        //blue 00bacb
        //red c23531
        //normal FF8F27
        Color color = new Color();
        if (dCurrentTemperature > GlobalValue.MaxLimitTemperature)
        {
            color.r = 0.7607843f;
            color.g = 0.2078431f;
            color.b = 0.1921569f;
            color.a = 1;
        }
        else if (dCurrentTemperature < GlobalValue.MinLimitTemperature)
        {
            color.r = 0;
            color.g = 0.7294118f;
            color.b = 0.7960784f;
            color.a = 1;
        }
        else
        {
            color.r = 1;
            color.g = 0.5607843f;
            color.b = 0.1529412f;
            color.a = 1;
        }
        
        Liquid_Temperature.GetVessel(0).color = color;
        Liquid_Temperature.UpdateData(0, 0, (float)dCurrentTemperature);
        Liquid_Temperature.UpdateData(1, 0, (float)dCurrentTemperature);
        Txt_Temperature.text = string.Format("{0}", dCurrentTemperature);
    }

    private void UpdateRecordTemperature(List<HistogramDataBase> listData)
    {
        Panel_6H.Refresh(listData);
        Panel_12H.Refresh(listData);
    }

	public void SetData(UI_Temperature_Data uiTemperatureData)
    {
        Liquid_Temperature.UpdateData(0, 0, uiTemperatureData.CurrentTemperature);

        double dMax1 = 0;
        double dMin1 = 0;
        double dMax2 = 0;
        double dMin2 = 0;
        List<HistogramDataBase> listData = new List<HistogramDataBase>();
        for (int i = 0; i < uiTemperatureData.TemperatureRecord.Count; i++)
        {
            HistogramDataBase data = new HistogramData_Temperature()
            {
                Current = uiTemperatureData.TemperatureRecord[i].Temperature,
                Timestamp = uiTemperatureData.TemperatureRecord[i].Timestamp
            };
            if (i < 6)
            {
                dMax1 = Math.Max(dMax1, Math.Abs(uiTemperatureData.TemperatureRecord[i].Temperature));
                dMin1 = Math.Max(dMin1, Math.Abs(uiTemperatureData.TemperatureRecord[i].Temperature));
            }
            dMax2 = Math.Max(dMax2, Math.Abs(uiTemperatureData.TemperatureRecord[i].Temperature));
            dMin2 = Math.Min(dMin2, Math.Abs(uiTemperatureData.TemperatureRecord[i].Temperature));
            listData.Add(data);
        }
        for (int i = 0; i < listData.Count; i++) 
        {
            ((HistogramData_Temperature)listData[i]).Max = i < 6 ? dMax1 : dMax2;
            ((HistogramData_Temperature)listData[i]).Min = i < 6 ? dMin1 : dMin2;
        }
        Panel_6H.Refresh(listData);
        Panel_12H.Refresh(listData);
    }
    
    

    public void UpdateRefreshTime(long lRefreshTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lRefreshTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}", datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }

}
